package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.order.IDeductionRecordsService;
import com.mdd.admin.validate.order.DeductionRecordsCreateValidate;
import com.mdd.admin.validate.order.DeductionRecordsSearchValidate;
import com.mdd.admin.validate.order.DeductionRecordsUpdateValidate;
import com.mdd.admin.vo.order.DeductionRecordsDetailVo;
import com.mdd.admin.vo.order.DeductionRecordsListedVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.DeductionRecords;
import com.mdd.common.mapper.order.DeductionRecordsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 还款计划减免记录实现类
 * @author LikeAdmin
 */
@Service
public class DeductionRecordsServiceImpl implements IDeductionRecordsService {

    @Resource
    DeductionRecordsMapper deductionRecordsMapper;

    /**
     * 还款计划减免记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderDeductionRecordsListedVo>
     */
    @Override
    public PageResult<DeductionRecordsListedVo> list(PageValidate pageValidate, DeductionRecordsSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<DeductionRecords> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        deductionRecordsMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:repaymentPlanNo@repayment_plan_no:str",
            "=:oldRent@old_rent:str",
            "=:newRent@new_rent:str",
            "=:oldLiquidatedDamages@old_liquidated_damages:str",
            "=:newLiquidatedDamages@new_liquidated_damages:str",
            "=:deductionNo@deduction_no:str"
        });

        IPage<DeductionRecords> iPage = deductionRecordsMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DeductionRecordsListedVo> list = new LinkedList<>();
        for(DeductionRecords item : iPage.getRecords()) {
            DeductionRecordsListedVo vo = new DeductionRecordsListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 还款计划减免记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OrderDeductionRecords
     */
    @Override
    public DeductionRecordsDetailVo detail(Long id) {
        DeductionRecords model = deductionRecordsMapper.selectById(id);

        Assert.notNull(model, "还款计划减免记录数据不存在");

        DeductionRecordsDetailVo vo = new DeductionRecordsDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 还款计划减免记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(DeductionRecordsCreateValidate createValidate) {
        DeductionRecords model = new DeductionRecords();
        model.setRepaymentPlanNo(createValidate.getRepaymentPlanNo());
        model.setOldRent(createValidate.getOldRent());
        model.setNewRent(createValidate.getNewRent());
        model.setOldLiquidatedDamages(createValidate.getOldLiquidatedDamages());
        model.setNewLiquidatedDamages(createValidate.getNewLiquidatedDamages());
        model.setDeductionNo(createValidate.getDeductionNo());
        deductionRecordsMapper.insert(model);
    }

    /**
     * 还款计划减免记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(DeductionRecordsUpdateValidate updateValidate) {
        DeductionRecords model = deductionRecordsMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setRepaymentPlanNo(updateValidate.getRepaymentPlanNo());
        model.setOldRent(updateValidate.getOldRent());
        model.setNewRent(updateValidate.getNewRent());
        model.setOldLiquidatedDamages(updateValidate.getOldLiquidatedDamages());
        model.setNewLiquidatedDamages(updateValidate.getNewLiquidatedDamages());
        model.setDeductionNo(updateValidate.getDeductionNo());
        deductionRecordsMapper.updateById(model);
    }

    /**
     * 还款计划减免记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        DeductionRecords model = deductionRecordsMapper.selectById(id);

        Assert.notNull(model, "数据不存在!");

        deductionRecordsMapper.deleteById(id);
    }

}
